package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.PageReqDto;
import com.itheima.common.PageResult6;
import com.itheima.entity.*;
import com.itheima.mapper.*;
import com.itheima.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class QuestionServiceImpl implements QuestionService {
    @Autowired
    private QuestionMapper questionMapper;
    @Autowired
    private OptionMapper optionMapper;
    @Autowired
    private QuestionsAuditMapper questionsAuditMapper;
    @Autowired
    private QuestionsRecordMapper questionsRecordMapper;
    @Autowired
    private CompanyMapper companyMapper;
    @Autowired
    private QuestionGroupMapper questionGroupMapper;

    @Autowired
    private SubjectsMapper subjectsMapper;

    @Autowired
    private SubjectsDirectorysMapper subjectsDirectorysMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public PageResult6 findAllBaseQues(PageReqDto pageReqDto,String email ) {
        LambdaQueryWrapper<User> wrapper3 = new LambdaQueryWrapper<>();
        wrapper3.eq(User::getEmail, email);
        User user = userMapper.selectOne(wrapper3);
        pageReqDto.setCreatorId(user.getId().intValue());
        PageResult6 pageResult = new PageResult6();
        Integer page = pageReqDto.getPage();
        Integer pagesize = pageReqDto.getPagesize();
        IPage<Question> p = new Page<>(page, pagesize);
        LambdaQueryWrapper<Question> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(pageReqDto.getKeyword() != null, Question::getQuestion, pageReqDto.getKeyword());
        wrapper.eq(pageReqDto.getSubjectID() != null, Question::getSubjectID, pageReqDto.getSubjectID());
        wrapper.eq(pageReqDto.getDifficulty() != null, Question::getDifficulty, pageReqDto.getDifficulty());
        wrapper.eq(pageReqDto.getQuestionType() != null, Question::getQuestionType, pageReqDto.getQuestionType());
        wrapper.eq(pageReqDto.getTags() != null, Question::getTags, pageReqDto.getTags());
        wrapper.eq(pageReqDto.getCity() != null, Question::getCity, pageReqDto.getCity());
        wrapper.eq(pageReqDto.getRemarks() != null, Question::getRemarks, pageReqDto.getRemarks());
        wrapper.eq(pageReqDto.getDirection() != null, Question::getDirection, pageReqDto.getDirection());
        wrapper.eq(pageReqDto.getCreatorId() != null, Question::getCreatorId, pageReqDto.getCreatorId());
        wrapper.eq(pageReqDto.getCatalogID() != null, Question::getCatalogID, pageReqDto.getCatalogID());
        wrapper.eq(pageReqDto.getChkState() != null, Question::getChkState, pageReqDto.getChkState());
        wrapper.eq(pageReqDto.getQuestionType() != null, Question::getQuestionType, pageReqDto.getQuestionType());
        String shortName = pageReqDto.getShortName();
        if (shortName != null) {
            LambdaQueryWrapper<Company> wrpper3 = new LambdaQueryWrapper<>();
            wrpper3.eq(Company::getShortName, shortName);
            Company company = companyMapper.selectOne(wrpper3);
            wrapper.eq(Question::getEnterpriseID, company.getId());
        }
        wrapper.eq(Question::getIsChoice, 0);
        IPage<Question> questionIPage = questionMapper.selectPage(p, wrapper);
        List<Question> records = questionIPage.getRecords();
        records.forEach(a -> {
            LambdaQueryWrapper<Option> wrapper2 = new LambdaQueryWrapper<>();
            wrapper2.eq(Option::getQuestionsID, a.getId());
            a.setOptions(optionMapper.selectList(wrapper2));
            Subjects subject = subjectsMapper.selectById(a.getSubjectID());
            SubjectsDirectorys subjectsDirectory = subjectsDirectorysMapper.selectById(a.getCatalogID());
            a.setCatalog(subjectsDirectory.getDirectoryName());
            a.setSubject(subject.getSubjectName());
            a.setCreator(user.getUsername());
        });
        long total = questionIPage.getTotal();
        pageResult.setPagesize(pagesize);
        pageResult.setPage(page);
        pageResult.setCounts((int) total);
        pageResult.setPages((int) questionIPage.getPages());
        pageResult.setItems(records);
        return pageResult;
    }

    @Override
    public void deleteById(Integer id) {
        questionMapper.deleteById(id);
        LambdaUpdateWrapper<Option> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Option::getQuestionsID, id);
        optionMapper.delete(wrapper);
    }

    @Override
    public void addQue(Question question, String email) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getEmail, email);
        User user = userMapper.selectOne(wrapper);
        question.setNumber(UUID.randomUUID().toString());
        question.setPublishState(0);
        question.setPublishDate(new Date());
        question.setChkState(0);
        question.setCreatorId(user.getId().intValue());
        question.setAddDate(new Date());
        questionMapper.insert(question);
        List<Option> options = question.getOptions();
        options.forEach(a -> {
            a.setQuestionsID(question.getId());
            optionMapper.insert(a);
        });
    }

    @Override
    public Question findById(Integer id) {
        Question question = questionMapper.selectById(id);
        LambdaQueryWrapper<Option> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Option::getQuestionsID, id);
        question.setOptions(optionMapper.selectList(wrapper));
        return question;
    }

    @Override
    public void modifyById(Integer id, Question question) {
        question.setId(id);
        questionMapper.updateById(question);
        LambdaUpdateWrapper<Option> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Option::getQuestionsID, question.getId());
        optionMapper.delete(wrapper);
        List<Option> options = question.getOptions();
        options.forEach(a -> {
            a.setQuestionsID(question.getId());
            optionMapper.insert(a);
        });
    }

    @Override
    public void changePublishState(Integer id, Integer publishState) {
        Question question = questionMapper.selectById(id);
        question.setPublishState(publishState);
        questionMapper.updateById(question);
    }

    @Override
    public List<QuestionsAudit> auditOpinions(Integer id) {
        LambdaQueryWrapper<QuestionsAudit> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(QuestionsAudit::getQuestionsId, id);
        return questionsAuditMapper.selectList(wrapper);
    }

    @Override
    public List<QuestionsRecord> setRecords(Integer id) {
        LambdaQueryWrapper<QuestionsRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(QuestionsRecord::getQuestionsID, id);
        return questionsRecordMapper.selectList(wrapper);
    }

    @Override
    public PageResult6 findQualityQues(PageReqDto pageReqDto, String email) {
        LambdaQueryWrapper<User> wrapper3 = new LambdaQueryWrapper<>();
        wrapper3.eq(User::getEmail, email);
        User user = userMapper.selectOne(wrapper3);
        PageResult6 pageResult = new PageResult6();
        Integer page = pageReqDto.getPage();
        Integer pagesize = pageReqDto.getPagesize();
        IPage<Question> p = new Page<>(page, pagesize);
        LambdaQueryWrapper<Question> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(pageReqDto.getKeyword() != null, Question::getQuestion, pageReqDto.getKeyword());
        wrapper.eq(pageReqDto.getSubjectID() != null, Question::getSubjectID, pageReqDto.getSubjectID());
        wrapper.eq(pageReqDto.getDifficulty() != null, Question::getDifficulty, pageReqDto.getDifficulty());
        wrapper.eq(pageReqDto.getQuestionType() != null, Question::getQuestionType, pageReqDto.getQuestionType());
        wrapper.eq(pageReqDto.getTags() != null, Question::getTags, pageReqDto.getTags());
        wrapper.eq(pageReqDto.getCity() != null, Question::getCity, pageReqDto.getCity());
        wrapper.eq(pageReqDto.getRemarks() != null, Question::getRemarks, pageReqDto.getRemarks());
        wrapper.eq(pageReqDto.getDirection() != null, Question::getDirection, pageReqDto.getDirection());
        wrapper.eq(pageReqDto.getCreatorId() != null, Question::getCreatorId, pageReqDto.getCreatorId());
        wrapper.eq(pageReqDto.getCatalogID() != null, Question::getCatalogID, pageReqDto.getCatalogID());
        wrapper.eq(pageReqDto.getChkState() != null, Question::getChkState, pageReqDto.getChkState());
        wrapper.eq(pageReqDto.getQuestionType() != null, Question::getQuestionType, pageReqDto.getQuestionType());
        String shortName = pageReqDto.getShortName();
        if (shortName != null) {
            LambdaQueryWrapper<Company> wrpper3 = new LambdaQueryWrapper<>();
            wrpper3.eq(Company::getShortName, shortName);
            Company company = companyMapper.selectOne(wrpper3);
            wrapper.eq(Question::getEnterpriseID, company.getId());
        }
        wrapper.eq(Question::getIsChoice, 1);
        IPage<Question> questionIPage = questionMapper.selectPage(p, wrapper);
        List<Question> records = questionIPage.getRecords();
        records.forEach(a -> {
            LambdaQueryWrapper<Option> wrapper2 = new LambdaQueryWrapper<>();
            wrapper2.eq(Option::getQuestionsID, a.getId());
            a.setOptions(optionMapper.selectList(wrapper2));
            Subjects subject = subjectsMapper.selectById(a.getSubjectID());
            SubjectsDirectorys subjectsDirectory = subjectsDirectorysMapper.selectById(a.getCatalogID());
            a.setCatalog(subjectsDirectory.getDirectoryName());
            a.setSubject(subject.getSubjectName());
            a.setCreator(user.getUsername());
        });
        long total = questionIPage.getTotal();
        pageResult.setPagesize(pagesize);
        pageResult.setPage(page);
        pageResult.setCounts((int) total);
        pageResult.setPages((int) questionIPage.getPages());
        pageResult.setItems(records);
        return pageResult;
    }

    @Override
    public PageResult6 findAllRandoms(PageReqDto pageReqDto) {
        String keyword = pageReqDto.getKeyword();
        Integer pagesize = pageReqDto.getPagesize();
        Integer page = pageReqDto.getPage();
        PageResult6 pageResult = new PageResult6();
        IPage<QuestionGroup> p = new Page<>(page, pagesize);
        LambdaQueryWrapper<QuestionGroup> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(keyword != null, QuestionGroup::getId, keyword);
        IPage<QuestionGroup> questionGroupIPage = questionGroupMapper.selectPage(p, wrapper);
        long total = questionGroupIPage.getTotal();
        pageResult.setPagesize(pagesize);
        pageResult.setPage(page);
        pageResult.setCounts((int) total);
        pageResult.setPages((int) questionGroupIPage.getPages());
        pageResult.setItems(questionGroupIPage.getRecords());
        return pageResult;
    }

    @Override
    public void deleteRandom(Integer id) {
        questionGroupMapper.deleteById(id);
    }

    @Override
    public void changeChoiceState(Integer id, Integer choiceState) {
        Question question = questionMapper.selectById(id);
        question.setIsChoice(choiceState);
        questionMapper.updateById(question);
    }

    @Override
    public void changeCheck(Integer id, Question que) {
        Question question = questionMapper.selectById(id);
        question.setChkState(que.getChkState());
        question.setChkRemarks(que.getChkRemarks());
        questionMapper.updateById(question);
    }

}
